#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <omp.h>

#include "array.h"
#include "sortingalgorithm.h"
#include "bitonicsort.h"
#include "bubblesort.h"
#include "evenoddmergesort.h"
#include "shearsort.h"

using namespace std;

void helpAndExit(int argc){
    cout << "Wrong number of arguments. Expected 4 but given " << argc << endl;
    cout << "Use: SORT_ALG_NAME INPUT_FILE THREAD_CNT, for example: bubble array10.txt 4" << endl;
    cout << "SORT_ALG_NAME = {'shear', 'bitonic', 'evenodd'}" << endl;
    exit(1);
}

int main(int argc, char * argv[])
{
    if (argc != 4)
        {
            helpAndExit(argc);
        }

        Array * a = new Array(argv[2]);
        SortingAlgorithm * sa = NULL;
        int threadCnt = atoi(argv[3]);

        if (!a)
            exit(1);

        if(strcmp("bubble", argv[1]) == 0)
        {
            sa = new BubbleSort();
        } else if(strcmp("shear", argv[1]) == 0)
        {
            sa = new ShearSort(threadCnt);
        } else if(strcmp("bitonic", argv[1]) == 0)
        {
            sa = new BitonicSort(threadCnt);
        } else if (strcmp("evenodd", argv[1]) == 0)
        {
            sa = new EvenOddMergeSort(threadCnt);
        } else
        {
            helpAndExit(argc);
        }

//        cout << "------ Unsorted Array ------" << endl;
//        a->printArray();

        double begin = omp_get_wtime();

        sa->sort(a);

        double end = omp_get_wtime();

//        cout << "------ Sorted Array ------" << endl;
//        a->printArray();

        cout << "Time elapsed: " << setprecision(6) << fixed << (end - begin) << " s." << endl;

        delete a;
        delete sa;

        return 0;
}
